<template>
  <div class="app">
    <div class="search-bar">
      <el-form :model="searchData" label-width="80px" style="padding: 0 20px;">
        <el-row :gutter="10">
          <el-col :span="6" :xs="12" :sm="12" :md="6">
            <el-form-item label="公司代码" style="margin-bottom: 5px;">
              <el-input v-model="searchData.cmhkId" size="medium" clearable @keyup.enter.native="initData(searchData)" />
            </el-form-item>
          </el-col>
          <el-col :span="6" :xs="12" :sm="12" :md="6">
            <el-form-item label="公司名称" style="margin-bottom: 5px;">
              <el-input v-model="searchData.companyChiName" size="medium" @keyup.enter.native="initData(searchData)" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6" :xs="12" :sm="12" :md="6">
            <el-form-item label="注册类型" style="margin-bottom: 5px;">
              <el-select v-model="searchData.registerType" size="medium" class="select-item" clearable @change="handleSearch">
                <el-option
                  v-for="item in registerTypeOption"
                  :key="item.code"
                  :label="item.value"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" :xs="12" :sm="12" :md="6">
            <div style="margin-top: 3px;">
              <el-button icon="el-icon-search" type="primary" size="small" @click="handleSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" size="small" @click="handleClear">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div ref="ref" :class="smallSize?'table-box':'table-box-small'">
      <tableComponent
        class="table"
        :height="height"
        :data="tableData || []"
        :columns="tableDetailColumns"
        @change="onTableChange"
        :table-option="tableOption"
        :page-option="searchData.page"
        :total="total">
        <template slot-scope="scope">
          <div class="options">
            <el-button type="text" @click="editItem(scope.scope.row)">查看</el-button>
          </div>
        </template>
      </tableComponent>
    </div>
  </div>
</template>

<script>
import tableComponent from '@/components/Table/table'
import { tableDetailColumns } from './js/index.js'
import { queryCompany } from '@/api/modules/propertyRight.js'
export default {
  components: {
    tableComponent
  },
  watch: {
    screenWidth: {
      handler(val) {
        this.windowWidth(val)
      },
      immediate: true
    }
  },
  data() {
    return {
      searchData: {
        cmhkId: '',
        companyChiName: '',
        registerType: '',
        page: {
          pageNum: 1,
          pageSize: 10
        }
      },
      height: null,
      tableData: [],
      tableDetailColumns,
      tableOption: {
        isShowOperate: true,
        operateWidth: '60px'
      },
      total: 0,
      registerTypeOption: [
        { value: '股份有限公司', code: '01' },
        { value: '有限责任公司', code: '02' },
        { value: '非公司制法人企业', code: '03' },
        { value: '合伙企业', code: '04' },
        { value: '分支机构', code: '05' }
      ],
      screenWidth: document.documentElement.clientWidth,
      smallSize: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.height = this.$refs.ref.offsetHeight
    })
    const that = this
    window.onresize = () => {
      that.height = that.$refs.ref.offsetHeight
      const val = document.documentElement.clientWidth
      that.screenWidth = val
    }
    this.initData(this.searchData)
  },
  destroyed(){
    window.onresize = null
  },
  methods: {
    initData(params) {
      queryCompany(params).then(res => {
        if (res.data.code === '0') {
          this.tableData = res.data.body.list
          this.total = res.data.body.total
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    // 分页操作
    onTableChange(val) {
      this.searchData.page = val
      this.initData(this.searchData)
    },
    editItem(row) {
      // console.log(row)
      const query = row
      this.$router.push({ path: '/detail', query })
    },
    // 查询
    handleSearch() {
      this.searchData.page.pageNum = 1
      this.initData(this.searchData)
    },
    // 重置查询条件
    handleClear() {
      this.searchData.cmhkId = ''
      this.searchData.companyChiName = ''
      this.searchData.registerType = ''
    },
    windowWidth(val) {
      if (val < 992) {
        this.smallSize = false
      } else {
        this.smallSize = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .app {
    height: 100%;
    .search-bar {
      width: calc(100% - 60px);
      background-color: #ffffff;
      margin-bottom: 10px;
      padding: 10px 30px;
    }
    .table-box {
      width: 100%;
      height: calc(100vh - 210px);
      .table {
        height: 100%;
      }
    }
    .table-box-small {
      width: 100%;
      height: calc(100vh - 255px);
      .table {
        height: 100%;
      }
    }
    .select-item {
      width: 100%;
    }
  }
</style>
<style>
  .table-row th {
    text-align: center;
  }
</style>
